//=============================================================================
#include "CPlayer.h"

//=============================================================================
CPlayer CPlayer::PlayerList[MAX_CLIENTS];

//=============================================================================
CPlayer::CPlayer() {
    UID     = "";

    ID      = -1;
    AreaID  = -1;

    State   = 0;

    X       = 0;
    Y       = 0;

    Width   = 0;
    Height  = 0;
}

//=============================================================================
bool CPlayer::OnLoad() {
    return true;
}

//-----------------------------------------------------------------------------
void CPlayer::OnRender() {
    if(ID == 0) {
        glColor3f(1, 0, 0);
    }else{
        glColor3f(0, 1, 0);
    }

    glBegin(GL_QUADS);
        glVertex3f(X, Y, 0);
        glVertex3f(X + 32, Y, 0);
        glVertex3f(X + 32, Y + 32, 0);
        glVertex3f(X, Y + 32, 0);
    glEnd();
}

//-----------------------------------------------------------------------------
void CPlayer::OnCleanup() {
    UID = "";
}

//=============================================================================
std::string CPlayer::Serialize() {
    std::string Return = Stringify(ID) + " " + Stringify(AreaID) + " " + Stringify(State) + " " + Stringify(X) + " " + Stringify(Y) + " " + Stringify(Width) + " " + Stringify(Height);

    return Return;
}

//-----------------------------------------------------------------------------
void CPlayer::Unserialize(std::string Data) {
    std::vector<std::string> Parts = StringifyExplode(Data, " ");

    X = atoi(Parts[1].c_str());
    Y = atoi(Parts[2].c_str());
}

//=============================================================================
std::vector<CPlayer*> CPlayer::GetPlayersInProximity() {
    std::vector<CPlayer*> List;

    for(unsigned int i = 0;i < MAX_CLIENTS;i++) {
        if(this->UID == PlayerList[i].UID)  continue;
        if(this->UID == "")                 continue;

        List.push_back(&PlayerList[i]);
    }

    return List;
}

//=============================================================================
int CPlayer::Connect(std::string UID) {
    CPlayer* NewPlayer = NULL;

    for(unsigned int i = 0;i < MAX_CLIENTS;i++) {
        if(PlayerList[i].UID == "") {
            NewPlayer = &PlayerList[i];
            NewPlayer->ID = i;
            break;
        }
    }

    if(NewPlayer == NULL) return false;

    if(NewPlayer->OnLoad() == false) {
        return false;
    }

    NewPlayer->UID = UID;

    return NewPlayer->ID;
}

//-----------------------------------------------------------------------------
void CPlayer::Disconnect(std::string UID) {
    for(unsigned int i = MAX_CLIENTS - 1;i >= 0;i--) {
        if(PlayerList[i].UID == UID) {
            PlayerList[i].OnCleanup();
            break;
        }
    }
}

//=============================================================================
